<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/1/9
 * Time: 下午3:39
 */

namespace Biz\model\trade;

class OrderRefundQueue extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_trade';

    const _DATABASE_ = 'shop_trade';

    const _TABLE_NAME = 'order_refund_queue';


    //是否系统操作退款
    const IS_SYS_NO = 0;//非系统操作退款
    const IS_SYS_YES = 1;//系统操作退款

    //退积分状态
    const REFUND_INTEGRAL_STATUS_NORMAL = 0;//未处理
    const REFUND_INTEGRAL_STATUS_COMPLETE = 1;//处理完毕

    //退钱状态
    const REFUND_RMB_STATUS_NORMAL = 0;//未处理
    const REFUND_RMB_STATUS_COMPLETE = 1;//处理完毕

    public static $orderReturnStatus = [
        self::REFUND_RMB_STATUS_NORMAL => '处理中',
        self::REFUND_RMB_STATUS_COMPLETE  => '已完成',
    ];

    //pay_way的值
    const REFUND_WAY_ALIPAY_APP = 'alipay';//支付宝APP支付
    const REFUND_WAY_WXPAY_APP = 'wechat';//微信APP支付
    const REFUND_WAY_MINIPROGRAM = 'miniprogram';//微信小程序
    const REFUND_WAY_TUAN_MINIPROGRAM = 'tuan_miniprogram';//节选微信小程序

    public static $payWayDesc = [
        self::REFUND_WAY_ALIPAY_APP => '支付宝',
        self::REFUND_WAY_WXPAY_APP => '微信',
        self::REFUND_WAY_MINIPROGRAM => '微信小程序',
        self::REFUND_WAY_TUAN_MINIPROGRAM => '节选微信小程序',
    ];

    //佣金状态
    const REFUND_COMMISSION_STATUS_NORMAL = 0;//未处理
    const REFUND_COMMISSION_STATUS_COMPLETE = 1;//处理完毕

    public static function getOrderRefundQueueList($field = '*', $orderRefundQueueWhere = [], $order, $limit = []){

        if (isset($orderRefundQueueWhere['payWay']) && $orderRefundQueueWhere['payWay']) {
            $where['AND']['refund_rmb_way'] = $orderRefundQueueWhere['payWay'];
        }
        if (isset($orderRefundQueueWhere['start_time']) && $orderRefundQueueWhere['start_time']) {
            $where['AND']['addtime[>]'] = $orderRefundQueueWhere['start_time'];
        }
        if (isset($orderRefundQueueWhere['end_time']) && $orderRefundQueueWhere['end_time']) {
            $where['AND']['addtime[<=]'] = $orderRefundQueueWhere['end_time'];
        }

        if (isset($orderRefundQueueWhere['orderNo']) && $orderRefundQueueWhere['orderNo']) {
            $where['AND']['order_no'] = $orderRefundQueueWhere['orderNo'];
        }
        if (isset($orderRefundQueueWhere['refundNo']) && $orderRefundQueueWhere['refundNo']) {
            $where['AND']['refund_order_no'] = $orderRefundQueueWhere['refundNo'];
        }
        if ($limit) {
            $where['LIMIT'] = $limit;
        }
        if (isset($orderRefundQueueWhere['status'])){

            $where['AND']['refund_rmb_status'] = $orderRefundQueueWhere['status'];
        }

        $where['ORDER'] = ['addtime' => 'DESC'];

        return self::select($field, $where);
    }

    public static function getOrderRefundQueueCount($orderRefundQueueWhere = []){
        if (isset($orderRefundQueueWhere['payWay']) && $orderRefundQueueWhere['payWay']) {
            $where['AND']['refund_rmb_way'] = $orderRefundQueueWhere['payWay'];
        }
        if (isset($orderRefundQueueWhere['start_time']) && $orderRefundQueueWhere['start_time']) {
            $where['AND']['addtime[>]'] = $orderRefundQueueWhere['start_time'];
        }
        if (isset($orderRefundQueueWhere['end_time']) && $orderRefundQueueWhere['end_time']) {
            $where['AND']['addtime[<=]'] = $orderRefundQueueWhere['end_time'];
        }
        if (isset($orderRefundQueueWhere['orderNo']) && $orderRefundQueueWhere['orderNo']) {
            $where['AND']['order_no'] = $orderRefundQueueWhere['orderNo'];
        }
        if (isset($orderRefundQueueWhere['refundNo']) && $orderRefundQueueWhere['refundNo']) {
            $where['AND']['refund_order_no'] = $orderRefundQueueWhere['refundNo'];
        }
        if (isset($orderRefundQueueWhere['status'])){

            $where['AND']['refund_rmb_status'] = $orderRefundQueueWhere['status'];
        }

        $where['ORDER'] = ['addtime' => 'DESC'];

        return self::count($where);
    }

    public static function getOrderRefundQueueRmbAmount($orderRefundQueueWhere){

        if (isset($orderRefundQueueWhere['payWay']) && $orderRefundQueueWhere['payWay']) {
            $where['AND']['refund_rmb_way'] = $orderRefundQueueWhere['payWay'];
        }
        if (isset($orderRefundQueueWhere['start_time']) && $orderRefundQueueWhere['start_time']) {
            $where['AND']['addtime[>]'] = $orderRefundQueueWhere['start_time'];
        }
        if (isset($orderRefundQueueWhere['end_time']) && $orderRefundQueueWhere['end_time']) {
            $where['AND']['addtime[<=]'] = $orderRefundQueueWhere['end_time'];
        }
        if (isset($orderRefundQueueWhere['orderNo']) && $orderRefundQueueWhere['orderNo']) {
            $where['AND']['order_no'] = $orderRefundQueueWhere['orderNo'];
        }
        if (isset($orderRefundQueueWhere['refundNo']) && $orderRefundQueueWhere['refundNo']) {
            $where['AND']['refund_order_no'] = $orderRefundQueueWhere['refundNo'];
        }
        if (isset($orderRefundQueueWhere['status'])){

            $where['AND']['refund_rmb_status'] = $orderRefundQueueWhere['status'];
        }
        $orderRefundRmdAmount = self::sum('refund_rmb', $where);
        $orderRefundShippingAmount = self::sum('refund_shipping_amount', $where);

        return $orderRefundShippingAmount + $orderRefundRmdAmount;
    }

    // 暂时使用方法 导出可能改为单页导出和导出全部
    public static function getOrderRefundQueue($field = '*', $orderRefundQueueWhere){

        if (isset($orderRefundQueueWhere['payWay']) && $orderRefundQueueWhere['payWay']) {
            $where['AND']['refund_rmb_way'] = $orderRefundQueueWhere['payWay'];
        }
        if (isset($orderRefundQueueWhere['start_time']) && $orderRefundQueueWhere['start_time']) {
            $where['AND']['addtime[>]'] = $orderRefundQueueWhere['start_time'];
        }
        if (isset($orderRefundQueueWhere['end_time']) && $orderRefundQueueWhere['end_time']) {
            $where['AND']['addtime[<=]'] = $orderRefundQueueWhere['end_time'];
        }
        if (isset($orderRefundQueueWhere['orderNo']) && $orderRefundQueueWhere['orderNo']) {
            $where['AND']['order_no'] = $orderRefundQueueWhere['orderNo'];
        }
        if (isset($orderRefundQueueWhere['refundNo']) && $orderRefundQueueWhere['refundNo']) {
            $where['AND']['refund_order_no'] = $orderRefundQueueWhere['refundNo'];
        }
        if (isset($orderRefundQueueWhere['status'])){

            $where['AND']['refund_rmb_status'] = $orderRefundQueueWhere['status'];
        }

        $where['ORDER'] = ['addtime' => 'DESC'];

        return self::select($field, $where);
    }
}